package cn.smbms.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatiesUtil {
    private  static  SqlSessionFactory factory = null ;

    // 静态代码块,类一旦加载，就会执行
    static {
        // 1 读取配置文件
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
            // 2 把配置文件给框架，
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static SqlSession createSession() {
        return  factory.openSession();
    }

    public static void closeSession( SqlSession sqlSession ) {
        if ( null != sqlSession )
        sqlSession.close();
    }



}
